package com.delivery.www.dao.DaoImpl;

import com.delivery.www.dao.BaseDao;
import com.delivery.www.dao.CollectDao;
import com.delivery.www.po.Collect;
import com.delivery.www.util.JdbcUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @author 12970
 */
public class CollectDaoImpl implements CollectDao {
    @Override
    public int doCollect(int userId, int dishId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {userId, dishId};
        String sql = "insert into collect (user_id,dish_id) values(?,?)";
        int i = 0;
        if (connection != null) {
            i = BaseDao.executeUpdate(connection, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return i;
    }

    @Override
    public List<Collect> getCollect(int userId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {userId};
        String sql = "select collect_id,user_id,dish_id from collect where user_id = ?";
        List<Collect> collects = null;
        if (connection != null) {
            collects = BaseDao.getList(connection, Collect.class, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return collects;
    }

    @Override
    public int cancelCollect(int userId, int dishId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {userId, dishId};
        String sql = "delete from collect where user_id=? and dish_id = ?";
        int i = 0;
        if (connection != null) {
            i = BaseDao.executeUpdate(connection, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return i;
    }

    @Override
    public Collect getOneCollect(int userId, int dishId) {
        Connection connection = JdbcUtils.getConnection();
        Collect collect = null;
        if (connection != null) {
            String sql = "select collect_id,user_id,dish_id from collect where user_id = ? and dish_id = ?";
            collect = BaseDao.getForOne(connection, Collect.class, sql, userId, dishId);
            JdbcUtils.closeResource(connection, null, null);
        }
        return collect;
    }
}
